package org.sport.dbImplement;

import org.sport.dbInterface.UserPrivateOperation;
import org.sport.model.entityModel.UserprivatetableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.UserprivateRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Allen on 2017/9/18.
 */

@Service
public class UserPrivateOpeImp implements UserPrivateOperation {


    @Autowired
    UserprivateRepository userprivateRepository;

    @Override
    public boolean isUserPriByUserPriId(UserprivatetableEntity userprivatet) {

        if(userprivateRepository.findOne(userprivatet.getId()) == null)
            return false;
        else
            return true;
    }

    @Override
    public UserprivatetableEntity findUserPriByUserId(UsertableEntity user) {

        System.out.println("******");
        UserprivatetableEntity userprivatet = new UserprivatetableEntity();
        userprivatet =  userprivateRepository.findUserPriByUserId(user.getId());
        return userprivatet;
    }

    @Override
    public List<UserprivatetableEntity> findAllUserPri() {

        List<UserprivatetableEntity> userpriList = userprivateRepository.findAll();
        return userpriList;
    }


    /**/
    @Override
    public boolean addUserPri(UserprivatetableEntity userprivatet) {


        if(userprivateRepository.saveAndFlush(userprivatet)  != null)
            return true;
        else
            return false;
    }



    @Override
    public boolean delUserPri(UserprivatetableEntity userprivatet) {

        if(userprivatet.getUsertableByUserId() != null)
        {
            userprivateRepository.delete(userprivatet);
            UserprivatetableEntity userprivatet2 = findUserPriByUserId(userprivatet.getUsertableByUserId());
            if(userprivatet2 != null)
                return false;
            else
                return true;
        }
        else
        {
            System.out.printf("传入的user为null");
            return false;
        }
    }

    /**/
    @Override
    public boolean updateUserPri(UserprivatetableEntity userprivatet) {


        System.out.println(userprivatet.toString());
        String userName                     = userprivatet.getUserName();
        Double totWalkDis                   = userprivatet.getTotWalkDis();
        Double totRunDis                    = userprivatet.getTotRunDis();
        Double totCycDis                    = userprivatet.getTotCycDis();
        Integer height                      = userprivatet.getHeight();
        Double weight                       = userprivatet.getWeight();
        Double bodyFatRate                  = userprivatet.getBodyFatRate();
        Double totSpoTim                    = userprivatet.getTotSpoTim();
        Integer sex                         = userprivatet.getSex();
        int id                              = userprivatet.getId();
        UsertableEntity usertableByUserId   = userprivatet.getUsertableByUserId();

        userprivateRepository.updateUserPrivateinfo(userName,totWalkDis,totRunDis
                                                    ,totCycDis
                                                    ,height
                                                    ,weight
                                                    ,bodyFatRate
                                                    ,totSpoTim
                                                    ,sex
                                                    ,usertableByUserId
                                                    );
        userprivateRepository.flush();
        return true;
    }
}
